//-----------------------------------------------------------------
//  Copyright 2010 Brady Wright and Above and Beyond Software
//	All rights reserved
//-----------------------------------------------------------------


using UnityEngine;
using System.Collections;
using System.Collections.Generic;



/// <remarks>
/// This implements SpriteBase and adds animation functionality
/// allowing the user to define their sprite animations using
/// individual, non-uniform sprite textures which will then be
/// compiled into a sprite atlas automatically.
/// </remarks>
public class lwEZPackedSpriteExt : PackedSprite
{
	public override Vector2 GetDefaultPixelSize(PathFromGUIDDelegate guid2Path, AssetLoaderDelegate loader)
	{
		if (staticTexPath == "")
			return Vector2.zero;

		Debug.LogWarning("stored static texture path : " + staticTexPath);
		WWW image = new WWW("file://" + lwBuildSettings.GetImagesFolder() + staticTexPath);
		while(!image.isDone);
		Texture2D tex = new Texture2D(image.texture.width, image.texture.height, TextureFormat.ARGB32, false);
		image.LoadImageIntoTexture(tex);
		Vector2 size = new Vector2(tex.width * (1f / (_ser_stat_frame_info.scaleFactor.x * 2f)), tex.height * (1f / (_ser_stat_frame_info.scaleFactor.y * 2f)));
		return size;
	}

	/// <summary>
	/// Collects all textures intended for packing,
	/// as well as sprite frames, together into a 
	/// standard form for processing.
	/// </summary>
	public override void Aggregate(PathFromGUIDDelegate guid2Path, LoadAssetDelegate load, GUIDFromPathDelegate path2Guid)
	{
		List<Texture2D> texList = new List<Texture2D>();
		List<CSpriteFrame> frameList = new List<CSpriteFrame>();

		string imageFolder = lwBuildSettings.GetImagesFolder();
		
		for (int i = 0; i < textureAnimations.Length; ++i)
		{
			textureAnimations[i].Allocate();

			textureAnimations[i].spriteFrames = new CSpriteFrame[textureAnimations[i].framePaths.Length];
			for (int j = 0; j < textureAnimations[i].spriteFrames.Length; ++j)
				textureAnimations[i].spriteFrames[j] = new CSpriteFrame();

			for (int j = 0; j < textureAnimations[i].framePaths.Length; ++j)
			{
				if (textureAnimations[i].framePaths[j].Length < 1)
					continue;
				if(!string.IsNullOrEmpty(textureAnimations[i].framePaths[j]) && System.IO.File.Exists(imageFolder + textureAnimations[i].framePaths[j])) {
					WWW image = new WWW("file://"+ imageFolder + textureAnimations[i].framePaths[j]);
					while(!image.isDone);
					Texture2D tex = new Texture2D(image.texture.width, image.texture.height, TextureFormat.ARGB32, false);
					image.LoadImageIntoTexture(tex);
					tex.name = textureAnimations[i].framePaths[j];
					texList.Add(tex);
					frameList.Add(textureAnimations[i].spriteFrames[j]);
				}
				else
				{
					Debug.LogWarning("Image not found : " + imageFolder + System.IO.Path.GetFullPath(textureAnimations[i].framePaths[j]));
					texList.Add(null);
					frameList.Add(textureAnimations[i].spriteFrames[j]);
				}
					
			}
		}
		
		// Get the static frame info:
		if (!string.IsNullOrEmpty(staticTexPath) && System.IO.File.Exists(lwBuildSettings.GetImagesFolder() + staticTexPath))
		{
			WWW image = new WWW("file://" + imageFolder + staticTexPath);
			while(!image.isDone);
			Texture2D tex = new Texture2D(image.texture.width, image.texture.height, TextureFormat.ARGB32, false);
			image.LoadImageIntoTexture(tex);
			tex.name = staticTexPath;
			texList.Add(tex);
		}
		else
			texList.Add(null);
		
		frameList.Add(_ser_stat_frame_info);
		
		sourceTextures = texList.ToArray();
		spriteFrames = frameList.ToArray();
	}
}
